package com.turkcell.dv.rest;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.eolcum.evocenter.Action;
import com.eolcum.evocenter.ActionBuffer;
import com.turkcell.dv.service.RequestScopedContext;

@RestController
@RequestMapping(value = "/loginService", headers = "Content-Type=application/json", produces = "application/json")
public class LoginService {
	
	@Autowired private ActionBuffer buffer;
	@Autowired private RequestScopedContext rsc;
	
	@RequestMapping(value= "/login", method = RequestMethod.GET)
	public void login() {
		if(rsc != null) {
			String userId = rsc.getUserId().toString();
			if(buffer.getActionList(userId) == null) {
				buffer.getActionMap().put(userId, new ArrayList<Action>());
			}
			Action a = new Action();
			a.setActionType(Action.ACTION_TYPE_POLLING);
			a.setTimeStamp(Calendar.getInstance().getTimeInMillis());
			a.setAction(Action.ACTION_GET_APARTMENT_INFO);
			a.setResponseURL("http://192.168.1.70:9091/apartmentService/setApartmentInfo");
			List<Action> actionList = buffer.getActionList(userId);
			boolean found = false;
			for(int i = 0; i < actionList.size(); i++) {
				if(actionList.get(i).getAction() == Action.ACTION_GET_APARTMENT_INFO) {
					found = true;
					actionList.set(i, a);
					break;
				}
			}
			if(!found) {
				actionList.add(a);
			}
		}
	}
}
